package com.fss.daoTestCases;

import java.util.*;

import junit.framework.TestCase;
import com.fssDataModels.*;
import com.fss.user.dao.UserDaoImpl;

public class TestUserDaoImpl extends TestCase{
	public TestUserDaoImpl(
	        String name) {
	        super(name);
	    }
	
	private UserDaoImpl db = new UserDaoImpl();
	
		/*========test right-data cases:============================*/		 
	    public void testInsert() {
	    	
	        TbUser newUser = new TbUser("user2","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
	        
	     
	        
	        db.insertUser(newUser);
	        
	        TbUser storedUser = db.getUser("user2");
	        
	        assertEquals(newUser.getDisplayName(),storedUser.getDisplayName());
	        
	        //clean up test data
	        db.deleteUser(storedUser);
	        
	    }
	    
	    public void testUpdate() {
	    	
	    	
	    	TbUser newUser = new TbUser("user3","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
	        
	        
	        
	        db.insertUser(newUser);
	        
	    	TbUser storedUser = db.getUser("user3");
	    	String newEmail="ansinhletrong@gmail.com";
	    	
	    	storedUser.setEmail(newEmail);
	    	
	    	db.updateUser(storedUser);
	    	
	    	TbUser updatedUser=db.getUser("user3");
	    	
	    	
	    	assertEquals(newEmail,updatedUser.getEmail());
	    	
	    	//clean up test data
	    	db.deleteUser(storedUser);
	    	
	    	
	    	
	        
	    }
	    
	    public void testDelete() {
	    	
	    	TbUser newUser = new TbUser("user4","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
	        
	        
	        
	        db.insertUser(newUser);
	        
	        
	        
	        TbUser storedUser = db.getUser("user4");
	        
	        db.deleteUser(storedUser);
	        
	        TbUser deletedUser = db.getUser("user4");
	        
	        assertEquals(null,deletedUser);
	        
	        
	    }
    
	    
	    /*============test wrong-data cases:==========================*/
	    /*++++++++must be not null data not included++++++++++*/
	    public void testInsertNoDisplayName() {
	    	TbUser newUser = new TbUser("user2",null,"123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
	        
	        
	        
	    	assertEquals(0,db.insertUser(newUser));
	        
	        TbUser storedUser = db.getUser("user2");
	    
	        
	        assertEquals(null,storedUser);
	    }
	    
	    public void testInsertNoPassword() {
	    	TbUser newUser = new TbUser("user2","ansinh",null,"ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
	        
	        
	        
	        db.insertUser(newUser);
	        
	        TbUser storedUser = db.getUser("user2");
	    
	        
	        assertEquals(null,storedUser);
	    }
	    
	    public void testInsertNoEmail() {
	    	TbUser newUser = new TbUser("user2","ansinh","123456",null,"as","as", new Date(),"2010-12-13");
	        
	        
	        
	        db.insertUser(newUser);
	        
	        TbUser storedUser = db.getUser("user2");
	    
	        
	        assertEquals(null,storedUser);
	    }
	    public void testInsertNoSecretQuestion() {
	    	TbUser newUser = new TbUser("user2","ansinh","123456","ansinh@ansinh.com",null,"as", new Date(),"2010-12-13");
	        
	        
	        
	        db.insertUser(newUser);
	        
	        TbUser storedUser = db.getUser("user2");
	    
	        
	        assertEquals(null,storedUser);
	    }
	    
	    public void testInsertNoSecretAnswer() {
	    	TbUser newUser = new TbUser("user2","ansinh","123456","ansinh@ansinh.com","as",null, new Date(),"2010-12-13");
	        
	        
	        
	        db.insertUser(newUser);
	        
	        TbUser storedUser = db.getUser("user2");
	    
	        
	        assertEquals(null,storedUser);
	    }
	    
	    public void testInsertNoCreatedTime() {
	    	TbUser newUser = new TbUser("user2","ansinh","123456","ansinh@ansinh.com","as","as",null,"2010-12-13");
	        
	        
	        
	        db.insertUser(newUser);
	        
	        TbUser storedUser = db.getUser("user2");
	    
	        
	        assertEquals(null,storedUser);
	    }
	    
	    public void testInsertNoLastLogin() {
	    	TbUser newUser = new TbUser("user2","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),null);
	        
	        
	        
	        db.insertUser(newUser);
	        
	        TbUser storedUser = db.getUser("user2");
	    
	        
	        assertEquals(null,storedUser);
	        
	    }
	    
	    public void testCheckExist()
	    {
	    	TbUser user = new TbUser();
	    	user.setUserId("ansinh");
	    	
	    	assertEquals(false,db.checkExist(user));
	    	
	    	user.setUserId("fss_admin1");
	    	
	    	assertEquals(true,db.checkExist(user));
	    	
	    }
	    //======================contact related test cases=================
	    public void testAddContact() {
	    	TbUser newUser = new TbUser("special user","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");	        	     	        
	        db.insertUser(newUser);
	        TbUser newUser2 = new TbUser("special user 2","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
	        db.insertUser(newUser2);
	        
	        assertEquals(1,db.addContact(newUser, newUser2));
	        
	        db.deleteContact(newUser, newUser2);
	        db.deleteUser(newUser);
	        db.deleteUser(newUser2);
	        
	        
	    }	    
	    
	    public void testGetContacts(){
	    	TbUser newUser = new TbUser("special user","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");	        	     	        
	        db.insertUser(newUser);
	        TbUser newUser2 = new TbUser("special user 2","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
	        db.insertUser(newUser2);
	        TbUser newUser3 = new TbUser("special user 3","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
	        db.insertUser(newUser3);
	        TbUser newUser4 = new TbUser("special user 4","ansinh","123456","ansinh@ansinh.com","as","as", new Date(),"2010-12-13");
	        db.insertUser(newUser4);
	        
	        
	        
	        db.addContact(newUser, newUser2);
	        db.addContact(newUser, newUser3);
	        db.addContact(newUser, newUser4);
	        
	        List contacts = db.getContacts(newUser);
	        assertEquals(3,contacts.size());
	        
	        
	        db.deleteContact(newUser, newUser2);
	        db.deleteContact(newUser, newUser3);
	        db.deleteContact(newUser, newUser4);
	        db.deleteUser(newUser);
	        db.deleteUser(newUser2);
	        db.deleteUser(newUser3);
	        db.deleteUser(newUser4);
	    	
	    }
	    
	    /*=================entry point of the testing program===========================*/
	    public static void main(String[] args) {
	        junit.textui.TestRunner.run(
	        		TestUserDaoImpl.class);
	    }
}
